#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

void CountSort(int* a, int n)
{
	int i = 0;
	int j = 0;
	int max, min;
	max = min = a[0];
	for (i = 0; i < n; i++)
	{
		if (a[i] > max)
			max = a[i];
		if (a[i] < min)
			min = a[i];
	}
	int range = max - min + 1;
	int* tmp = (int*)calloc(range, sizeof(a[i]));
	for (i = 0; i < n; i++)
	{
		tmp[a[i]]++;
	}
	for (i = 0; i < range; i++)
	{
		while (tmp[i]--)
		{
			a[j] = i + min;
			j++;
		}
	}
}

int main()
{
	int a[] = { 5,2,1,0,1,5,9,3 };
	CountSort(a, sizeof(a) / sizeof(a[0]));
	for (int i = 0; i < 8; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}